﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Caching;
using Telerik.Web.UI;
using System.Data.Common;
using System.Data;
using System.Web.Services;

public partial class Index : System.Web.UI.Page
{
    private Cache cache = HttpContext.Current.Cache;
    Control mainControl;
    DatabaseHelper DB = new DatabaseHelper();
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
   {
        string strURL = "";
        string strID = Request.QueryString["ID"];
        if (Session["isLogin"] != null)
        {
            if (Session["UserName"].ToString() != "admin")
            {
                if (strID != null && strID != "")
                {
                    DB.ClearParameter();

                    DbParameter paramParentID = DB.Parameter;
                    paramParentID.ParameterName = "UserId";
                    paramParentID.DbType = DbType.String;
                    paramParentID.Value = Session["UserName"];

                    DbParameter paramUserID = DB.Parameter;
                    paramUserID.ParameterName = "FunctionId";
                    paramUserID.DbType = DbType.String;
                    paramUserID.Value = Utils.KillChars(strID) == null ? "0" : Utils.KillChars(strID);

                    DB.AddParameter(paramParentID);
                    DB.AddParameter(paramUserID);

                    dt = DB.ExecuteDataTable("UserRole_CheckId", CommandType.StoredProcedure, ConnectionState.CloseOnExit);

                    if (dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0][0].ToString() == "F")
                        {
                            mainControl = LoadControl("~/Components/Error.ascx");
                            phContent.Controls.Add(mainControl);
                            Session["ErrorMessage"] = "Bạn không có quyền truy cập trang này!";
                            //SiteMap.CurrentNode = "";
                            return;
                        }
                    }
                    else
                    {
                        mainControl = LoadControl("~/Components/Error.ascx");
                        phContent.Controls.Add(mainControl);
                        Session["ErrorMessage"] = "Bạn không có quyền truy cập trang này!";

                        return;
                    }
                }
            }
            if (strID != null)
            {
                strURL = Utils.GetURL(strID);
                mainControl = LoadControl(strURL);
            }
            else
            {
                //string _strURL = LoadControl("~/Components/Welcome.ascx");
                mainControl = LoadControl("~/Components/Welcome.ascx");
            }

        }
        else
        {
            mainControl = LoadControl("~/Admin/Components/Permission/Login.ascx");
        }

        SiteMap.CurrentNode.ReadOnly = false;
        DB.ClearParameter();
        if (strID == "" || strID == null)
        {
            strID = "0";
        }
        DB.AddParameter("@ID", strID);
        DataTable dtSiteMap = new DataTable();
        dtSiteMap = DB.ExecuteDataTable("Function_Load", CommandType.StoredProcedure, ConnectionState.CloseOnExit);
        if (dtSiteMap.Rows.Count > 0)
        {
            SiteMap.CurrentNode.Title = dtSiteMap.Rows[0]["FunctionName"].ToString().Trim();
            //SiteMap.CurrentNode.Url = "~/Default.aspx?ID=" + dtSiteMap.Rows[0]["ID"].ToString().Trim();
        }
        else
        {
            SiteMap.CurrentNode.Title = "";
            //SiteMap.CurrentNode.Url = "Default.aspx";
        }
        SiteMap.RootNode.ReadOnly = false;
        SiteMap.RootNode.Title = "Trang chủ";
        this.Page.Header.Title = SiteMap.CurrentNode.Title;
        phContent.Controls.Add(mainControl);
    }
    [WebMethod()]
    public static string GetTime()
    {
        return DateTime.Now.ToString("HH:mm:ss");
    }
}